package com.winter.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;
import com.winter.mapper.CompanyUserMapper;
import com.winter.mapper.PeoplejobsMapper;
import com.winter.model.Peoplejobs;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class PeoplejobsService {

	@Autowired
	public PeoplejobsMapper peoplejobsMapper;
	
	@Autowired
	public CompanyUserMapper companyUserMapper;
	
	/**
	 *  投递简历
	 * @param record
	 * @return
	 */
	public Result InsertDelivery(Peoplejobs record){
		    if (record.getPersonCareerId() == null) {
		    	return Result.failure(ResultCode.FAILURE,"简历Id不能为空");
		    }
			if (record.getPositionId() == null) {
				return Result.failure(ResultCode.FAILURE,"职位Id不能为空");
			}
			if (Strings.isNullOrEmpty(record.getCreateUser())) {
				return Result.failure(ResultCode.FAILURE,"创建人不能为空");
			}
			Peoplejobs peoplejobs = peoplejobsMapper.queryById(null, record.getPersonCareerId(), record.getPositionId(), record.getCreateUser(), "1");
			if (peoplejobs != null) {
				return Result.failure(ResultCode.FAILURE,"请勿重复发送投递简历信息");
			}
			record.setIsvalid("1");//设置为有效
			record.setMatchresult("1");//撮合状态 DC_MATCH_STATUS 1-正在撮合 2-撮合成功 3-撮合失败
		    record.setJobtype("1");//求职类型 1应聘，2聘请
			record.setUpdateUser(record.getCreateUser());
			record.setUpdatetime(new Date());
			record.setCreatetime(new Date());
			int j = peoplejobsMapper.insertSelective(record);
			if (j <= 0) {
		    	return Result.failure(ResultCode.FAILURE,"投递简历失败");
			}
		 return Result.success();
		}
	
	/**
	 *  聘请
	 * @param record
	 * @return
	 */
	public Result InsertEmploy(Peoplejobs record){
		if (record.getPersonCareerId() == null) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "简历Id不能为空");
		}
		if (record.getPositionId() == null) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "职位Id不能为空");
		}
		if (Strings.isNullOrEmpty(record.getCreateUser())) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "创建人不能为空");
		}
		if (Strings.isNullOrEmpty(record.getUserType())) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "会员类型不能为空");
		}
		Peoplejobs peoplejobs = peoplejobsMapper.queryById(null, record.getPersonCareerId(), record.getPositionId(), record.getCreateUser(), "2");
		if (peoplejobs != null) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "请勿重复发送聘请信息");
		}
		if (!Strings.isNullOrEmpty(record.getUserType())) {
			if (record.getUserType().equals("0")) {
				return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED,"权限不足，禁止操作");
			}
			int count = peoplejobsMapper.querySumCount(record.getCreateUser(), "2");
			if (record.getUserType().equals("1")) {
				if (count >= 5) {
					return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED,"权限不足，禁止操作");
				}
			}
			if (record.getUserType().equals("2")) {
				if (count >= 5) {
					return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED,"权限不足，禁止操作");
				}
			}
		}
		record.setIsvalid("1");//设置为有效
		record.setMatchresult("1");//撮合状态 DC_MATCH_STATUS 1-正在撮合 2-撮合成功 3-撮合失败
		record.setJobtype("2");//求职类型 1应聘，2聘请
		record.setUpdateUser(record.getCreateUser());
		record.setUpdatetime(new Date());
		record.setCreatetime(new Date());
		int j = peoplejobsMapper.insertSelective(record);
		if (j <= 0) {
			return Result.failure(ResultCode.SYSTEM_UNAUTHORIZED, "聘请失败");
		}
		return Result.success();
	}
	
}
